package ${packageName}<#if moduleName??>.${moduleName}</#if>.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  ${packageName}.base.controller.BaseController;
import  ${packageName}.base.entity.RespEntity;
import  ${packageName}<#if moduleName??>.${moduleName}</#if>.entity.${beanName}Entity;
import  ${packageName}<#if moduleName??>.${moduleName}</#if>.service.${beanName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/${objectName}")
@CrossOrigin
public class ${beanName}Controller extends BaseController {

    @Autowired
    private ${beanName}Service ${objectName}Service;

    /**
    * 查询数据
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("${moduleName}:${objectName}:query")
    public RespEntity query(${beanName}Entity ${objectName}Entity){
        try {
            return new RespEntity("查询成功",${objectName}Service.query(${objectName}Entity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("${moduleName}:${objectName}:queryByPage")
    public RespEntity queryByPage(${beanName}Entity ${objectName}Entity){
        try {
            PageHelper.startPage(${objectName}Entity.getPageNo(),${objectName}Entity.getPageSize());
            List<${beanName}Entity> ${objectName}List = ${objectName}Service.query(${objectName}Entity);
            PageInfo<${beanName}Entity> pageInfo = new PageInfo<${beanName}Entity>(${objectName}List);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("${moduleName}:${objectName}:insert")
    public RespEntity insert(${beanName}Entity ${objectName}Entity){
        try {
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}CreateTime(new Date());
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}CreateUser(this.getCurrentUser().getUserId());
            ${objectName}Service.insert(${objectName}Entity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("${moduleName}:${objectName}:update")
    public RespEntity update(${beanName}Entity ${objectName}Entity){
        try {
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}UpdateTime(new Date());
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}UpdateUser(this.getCurrentUser().getUserId());
            ${objectName}Service.update(${objectName}Entity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("${moduleName}:${objectName}:delete")
    public RespEntity delete(${beanName}Entity ${objectName}Entity){
        try {
            if(${objectName}Entity.get${tableConfiguration.keyProperty?cap_first}() == null){
                return new RespEntity("删除失败");
            }
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}UpdateTime(new Date());
            ${objectName}Entity.set${tableConfiguration.fieldPrefix?cap_first}UpdateUser(this.getCurrentUser().getUserId());
            ${objectName}Service.logicalDelete(${objectName}Entity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
    * 根据ID查询
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/getById")
    @RequiresPermissions("${moduleName}:${objectName}:getById")
    public RespEntity getById(${beanName}Entity ${objectName}Entity){
        try {
            if(${objectName}Entity.get${tableConfiguration.keyProperty?cap_first}() == null){
                return new RespEntity("请输入id查询");
            }
            return new RespEntity("查询成功",${objectName}Service.getById(${objectName}Entity.get${tableConfiguration.keyProperty?cap_first}()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 根据实体查询
    * @param ${objectName}Entity
    * @return
    */
    @PostMapping("/get")
    @RequiresPermissions("${moduleName}:${objectName}:get")
    public RespEntity get(${beanName}Entity ${objectName}Entity){
        try {
            return new RespEntity("查询成功",${objectName}Service.get(${objectName}Entity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }
}